package com.mediacloud.app.newsmodule.activity.microlive;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.mediacloud.app.cloud.ijkplayersdk.video.VideoPlayer;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.microlive.LivesType3;
import com.mediacloud.app.newsmodule.model.VideoPlayerListenerImpl;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;


/**
 *
 */

public class ItemVideoAdapter extends BaseRecyclerAdapter<LivesType3> {

    int currentIndex = -1;
    VideoPlayer player;

    public ItemVideoAdapter(Context context, VideoPlayer videoPlayer) {
        super(context);
        player = videoPlayer;
        player.setOnPlayerListener(new VideoPlayerListenerImpl() {

            @Override
            public void onResume(int currentMediaIndex) {
                super.onResume(currentMediaIndex);
                notifyDataSetChanged();
            }

            @Override
            public void onpause(int currentMediaIndex) {
                super.onpause(currentMediaIndex);
                Log.w("J8ItemVideoAdapter", "onpause  currentIndex :: " + currentIndex);
                notifyDataSetChanged();
            }

            @Override
            public void oncomplete(int currentMediaIndex) {
                super.oncomplete(currentMediaIndex);
                Log.w("J8ItemVideoAdapter", "oncomplete  currentIndex :: " + currentIndex);
                notifyDataSetChanged();
            }

            @Override
            public void onPrepared(int currentMediaIndex) {
                super.onPrepared(currentMediaIndex);
                if (currentIndex == -1 && player != null && player.data instanceof Integer) {
                    currentIndex = (int) player.data;
                }
                Log.w("J8ItemVideoAdapter", "onPrepared  currentIndex :: " + currentIndex);
                notifyDataSetChanged();
            }
        });
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.microitemvideo, parent, false);
        final JItemVideo jItemVideo = new JItemVideo(view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (itemClickListener != null) {
                    try {
                        int index = Integer.valueOf(v.getTag() + "");
                        Log.w("J8ItemVideoAdapter", "index  = " + index + "  currentIndex :: " + currentIndex);
                        if (index == currentIndex) {
                            String videoPath = getItem(index).getVideopath();
                            if (!TextUtils.isEmpty(videoPath)) {
                                if (player.isPlaying())
                                    player.pause();
                                else if (player.isComplete()) {
                                    player.setAutoPlay(true);
                                    player.playVideobj(player.currentMediaIndex);
                                    player.resume_button_playstatus();
                                } else if (!player.isAutoPlay()) {
                                    player.setAutoPlay(true);
                                    player.playVideobj(player.currentMediaIndex);
                                } else
                                    player.start();
                            }
                        } else {
                            player.setAutoPlay(true);
                            player.playVideobj(player.currentMediaIndex);
                            player.resume_button_playstatus();
                            currentIndex = index;
                        }
                        player.data = index;
                        itemClickListener.onItemClick(index, true, ItemVideoAdapter.this);
                        notifyDataSetChanged();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
        });
        return jItemVideo;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        JItemVideo j86666 = (JItemVideo) holder;
        holder.itemView.setTag(position);
        j86666.hamapiImage.defaultBackGroundColor = AppFactoryGlobalConfig.getDefaultImagLoadBackground();
        j86666.hamapiImage.defaultDrawable = AppFactoryGlobalConfig.getDefaultImageLoadDrawable();
        j86666.play.setPaintColor(Color.WHITE);
        j86666.center_play.setBackgroundResource(R.drawable.icon_stop);
        if (getItem(position) != null)
            j86666.hamapiImage.load(getItem(position).getImagepath());
        else
            j86666.hamapiImage.setDefaultRes(true);
        if (currentIndex == position && player.isPlaying()) {
            j86666.center_play.setVisibility(View.GONE);
            j86666.play.start();
            j86666.rlLive.setVisibility(View.VISIBLE);
        } else {
            j86666.center_play.setVisibility(View.VISIBLE);
            j86666.play.pause();
            j86666.rlLive.setVisibility(View.GONE);
        }
    }
}